package com.gj.factoryclass.service.Impl;

import com.gj.factoryclass.mapper.FactoryClassMapper;
import com.gj.factoryclass.model.*;
import com.gj.factoryclass.service.FactoryClassService;
import com.gj.subclass.enumpackge.StatusEnum2;
import com.gj.utli.AllRecords;
import com.gj.utli.ResultBuilderVo;
import com.gj.utli.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Tangyunhen on 2021-06-27.
 */
@Service
public class FactoryClassServiceImpl implements FactoryClassService{

    @Autowired
    private FactoryClassMapper factoryClassMapper;

    //分页查询
    @Override
    public ResultVo queryPage(FactoryClassServiceVo factoryClassServiceVo) {
        //查询数据
        List<FactoryClassVo> factoryClassVoList = factoryClassMapper.queryData(factoryClassServiceVo);
        for (FactoryClassVo factoryClassVo:factoryClassVoList){
/*          if(factoryClassVo.getEnabled()==true){
              factoryClassVo.setEnabledName("启用");
            }else if (factoryClassVo.getEnabled()==false){
              factoryClassVo.setEnabledName("禁用");
            }*/
            Boolean status = factoryClassVo.getEnabled();
            Integer status2=status?1:0;
            factoryClassVo.setEnabledName(StatusEnum2.getStatusName(status2));
        }
        //查询数量
        int count = factoryClassMapper.queryCount(factoryClassServiceVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(factoryClassServiceVo.getPageIndex());
        allRecords.setPageSize(factoryClassServiceVo.getPageSize());
        allRecords.resetTotalNumber(count);
        allRecords.setTotalNumber(count);
        allRecords.setDataList(factoryClassVoList);
        return ResultBuilderVo.success(allRecords);
    }

    //添加
    @Override
    @Transactional
    public ResultVo add(@Valid FactoryClassAddVo factoryClassAddVo) {
        //名称编码不能重复
        FactoryClassVo factoryClassVo1=factoryClassMapper.queryByLegalPlantCode(factoryClassAddVo.getLegalPlantCode());
        if (factoryClassVo1!=null){
            return ResultBuilderVo.error("工厂已经存在了");
        }

        FactoryClassVo factoryClassVo=new FactoryClassVo();
        BeanUtils.copyProperties(factoryClassAddVo,factoryClassVo);//把左边参数赋值给右边
        Date nowDate =new Date();
        factoryClassVo.setCreateTime(nowDate);
        factoryClassVo.setCreateName("项羽");
        factoryClassVo.setUpdateTime(nowDate);
        factoryClassVo.setUpdateName("项羽");
        factoryClassMapper.insert(factoryClassVo);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo update(@Valid FactoryClassUpdateVo factoryClassUpdateVo) {
        FactoryClassVo factoryClassVo1 = factoryClassMapper.queryByLegalPlantCode(factoryClassUpdateVo.getLegalPlantCode());
        if (factoryClassVo1==null){
            return  ResultBuilderVo.error("修改对象不存在");
        }
        if (factoryClassVo1.getEnabled()){
            return ResultBuilderVo.error("禁用状态下才能修改");
        }

        FactoryClassVo factoryClassVo=new FactoryClassVo();
        BeanUtils.copyProperties(factoryClassUpdateVo,factoryClassVo);//把左边参数赋值给右边
        Date nowDate =new Date();
        factoryClassVo.setUpdateTime(nowDate);
        factoryClassVo.setUpdateName("项羽");
        factoryClassMapper.update(factoryClassVo);
        return ResultBuilderVo.success();
    }

   @Override
   @Transactional //事务
   public ResultVo updateStatus(@Valid FactoryClassUpdateStatusVo factoryClassUpdateStatusVo) {
       FactoryClassVo factoryClassVo1 = factoryClassMapper.queryByLegalPlantCode(factoryClassUpdateStatusVo.getLegalPlantCode());
       if (factoryClassVo1==null){
           return  ResultBuilderVo.error("修改对象不存在");
       }
        FactoryClassVo factoryClassVo=new FactoryClassVo();
        BeanUtils.copyProperties(factoryClassUpdateStatusVo,factoryClassVo);//把左边参数赋值给右边

        factoryClassMapper.updateStatus(factoryClassVo);
        return ResultBuilderVo.success();
    }


}




